package com.yaoli.niuke;

import com.yaoli.ajob.ListNode;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by will on 2017/6/7.
 */
public class _027_Test {
    public static void main(String[] args) {
        ListNode node1 = new ListNode(1);
        ListNode node2 = new ListNode(2);
        ListNode node3 = new ListNode(3);
        ListNode node4 = new ListNode(3);
        ListNode node5 = new ListNode(4);
        ListNode node6 = new ListNode(4);
        ListNode node7 = new ListNode(5);

        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;
        node5.next = node6;
        node6.next = node7;

        deleteDuplication(node1);

    }


    public static ListNode deleteDuplication(ListNode pHead){
        ListNode tmp = pHead;

        Map<Integer,Integer> map = new HashMap<Integer,Integer>();

        while(tmp!=null){
            if(!map.containsKey(tmp.val)){
                map.put(tmp.val,1);
            }else{
                map.put(tmp.val,map.get(tmp.val)+1);
            }
            tmp = tmp.next;
        }

        ListNode tmp2 = pHead;
        ListNode pre = new ListNode(-1);
        ListNode res = pre;
        pre.next = tmp2;

        while(tmp2 != null){
            if(map.get(tmp2.val) > 1){
                pre.next = tmp2.next;
                tmp2 = tmp2.next;
            }else{
                pre = tmp2;
                tmp2 = tmp2.next;
            }
        }

        return res.next;


    }
}
